package com.data.common.api.vo;

import com.data.common.constant.CommonConstant;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

/**
 *   自定义CRM 接口返回数据格式
 * @author cg
 * @email cg@163.com
 * @date  2021年12月31日
 */
@SuppressWarnings("ALL")
@Data
@ApiModel(value="接口返回对象", description="接口返回对象")
public class CrmResult<T> implements Serializable {

	private static final long serialVersionUID = 1L;


	/**
	 * 返回处理消息
	 */
	@ApiModelProperty(value = "返回处理消息" ,example = "success")
	private String msg = "";

	/**
	 * 异常信息描述
	 */
	@ApiModelProperty(value = "errorMsg")
	private String errorMsg;
	/**
	 * 返回代码 0 响应成功
	 */
	@ApiModelProperty(value = "返回代码" , example = "0")
	private Integer code = 0;

	/**
	 * 返回数据对象 data
	 */
	@ApiModelProperty(value = "返回数据对象")
	private T data;

	/**
	 * 时间戳
	 */
	@ApiModelProperty(value = "时间戳")
	private long timestamp = System.currentTimeMillis();

	public CrmResult() {
	}

    /**
     * 兼容VUE3版token失效不跳转登录页面
     * @param code
     * @param msg
     */
	public CrmResult(Integer code, String msg) {
		this.code = code;
		this.msg= msg;
	}
	
	public CrmResult<T> success(String msg) {
		this.msg = msg;
		this.code = 0;
		return this;
	}

	//@Deprecated
	public static CrmResult<Object> ok() {
		CrmResult<Object> r = new CrmResult<Object>();
		r.setCode(0);
		return r;
	}

	//@Deprecated
	public static CrmResult<Object> ok(String msg) {
		CrmResult<Object> r = new CrmResult<Object>();
		r.setCode(0);
		r.setMsg(msg);
		return r;
	}

	//@Deprecated
	public static CrmResult<Object> ok(Object data) {
		CrmResult<Object> r = new CrmResult<Object>();
		r.setCode(0);
		r.setData(data);
		return r;
	}

	public static<T> CrmResult<T> OK() {
		CrmResult<T> r = new CrmResult<T>();
		r.setCode(0);
		return r;
	}

	public static<T> CrmResult<T> OK(String msg) {
		CrmResult<T> r = new CrmResult<T>();
		r.setCode(0);
		r.setMsg(msg);
		//Result OK(String msg)方法会造成兼容性问题 issues/I4IP3D
		r.setData((T) msg);
		return r;
	}

	public static<T> CrmResult<T> OK(T data) {
		CrmResult<T> r = new CrmResult<T>();
		r.setCode(0);
		r.setData(data);
		return r;
	}

	public static<T> CrmResult<T> OK(String msg, T data) {
		CrmResult<T> r = new CrmResult<T>();
		r.setCode(0);
		r.setMsg(msg);
		r.setData(data);
		return r;
	}

	public static<T> CrmResult<T> error(String msg, T data) {
		CrmResult<T> r = new CrmResult<T>();
		r.setCode(CommonConstant.SC_INTERNAL_SERVER_ERROR_500);
		r.setMsg(msg);
		r.setData(data);
		return r;
	}

	public static CrmResult<Object> errorMsg(String errorMsg) {
		return error(CommonConstant.SC_INTERNAL_SERVER_ERROR_500, "网络异常",errorMsg);
	}

	public static CrmResult<Object> error(String msg) {
		return error(CommonConstant.SC_INTERNAL_SERVER_ERROR_500, msg);
	}
	
	public static CrmResult<Object> error(int code, String msg) {
		CrmResult<Object> r = new CrmResult<Object>();
		r.setCode(code);
		r.setMsg(msg);
		return r;
	}

	public static CrmResult<Object> error(int code, String msg, String errorMsg) {
		CrmResult<Object> r = new CrmResult<Object>();
		r.setCode(code);
		r.setMsg(msg);
		r.setErrorMsg(errorMsg);
		return r;
	}


	public CrmResult<T> error500(String msg) {
		this.msg = msg;
		this.code = CommonConstant.SC_INTERNAL_SERVER_ERROR_500;
		return this;
	}
	/**
	 * 无权限访问返回结果
	 */
	public static CrmResult<Object> noauth(String msg) {
		return error(401, msg);
	}

	@JsonIgnore
	private String onlTable;

}